package com.bingo.rbac.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 获取spring上下文
 *
 * @author bingo
 * @create 2017-12-03 11:12
 **/
@Component
public class ApplicationContextHelper implements ApplicationContextAware {

    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context =applicationContext;
    }

    public static <T> T popBean(Class<T> clazz) {
        if (context == null) {
            return null;
        }
        return context.getBean(clazz);
    }

    public static <T> T popBean(String name, Class<T> clazz) {
        if (context == null) {
            return null;
        }
        return context.getBean(name, clazz);
    }

}
